//
//  NSDate+LHDate.m
//  BaiSi
//
//  Created by mac on 16/11/4.
//  Copyright © 2016年 DeYi. All rights reserved.
//

#import "NSDate+LHDate.h"

@implementation NSDate (LHDate)

- (BOOL)isThisYear
{
    // 获取当前时间
    NSDate *currentDate = [NSDate date];
    // 日历类
    NSCalendar *calendar = [NSCalendar currentCalendar];
    // 获取当前日期组件
    NSDateComponents *curComp = [calendar components:NSCalendarUnitYear fromDate:currentDate];
    
    // 获取需要判断日期年份
    NSDateComponents *comp = [calendar components:NSCalendarUnitYear fromDate:self];
    
    return curComp.year == comp.year;
    
}
- (BOOL)isThisToday
{
    // 日历类
    NSCalendar *calendar = [NSCalendar currentCalendar];
    
    return  [calendar isDateInToday:self];
}

- (BOOL)isThisYesterday
{
    // 日历类
    NSCalendar *calendar = [NSCalendar currentCalendar];
    
    return [calendar isDateInYesterday:self];
}

// 获取两个日期差值
- (NSDateComponents *)deltaWithNow
{
    // 获取当前时间
    NSDate *currentDate = [NSDate date];
    // 日历类
    NSCalendar *calendar = [NSCalendar currentCalendar];
    
    // 获取两个日期差值
    return  [calendar components:NSCalendarUnitHour | NSCalendarUnitMinute fromDate:self toDate:currentDate options:NSCalendarWrapComponents];
}

/**
 *  获得与当前时间的差距
 */
- (NSDate *)dateWithYMD
{
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    fmt.dateFormat = @"yyyy-MM-dd";
    NSString *selfStr = [fmt stringFromDate:self];
    return [fmt dateFromString:selfStr];
}

@end
